package com.bgds.sys.config.web;

import com.bgds.sys.common.constant.BizConstant;
import com.bgds.sys.config.interceptor.web.AuthorityInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 弃用
 * @author lj
 */
@Slf4j
//@Configuration
@Deprecated
public class WebAppConfig  extends WebMvcConfigurationSupport {
    /**
     本地路径
     */
    @Value("${mapper.localUrl}")
    private String localUrl;
    /**
     * 服务器路径
     */
    @Value("${mapper.serverUrl}")
    private String serverUrl;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        registry.addInterceptor(new AuthorityInterceptor()).addPathPatterns("/**");


        super.addInterceptors(registry);

    }

    /**
     * 防止@EnableMvc把默认的静态资源路径覆盖了，手动设置的方式
     *
     * @param registry
     */
    @Override
    public  void addResourceHandlers(ResourceHandlerRegistry registry) {
//        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        super.addResourceHandlers(registry);

        String os = System.getProperty("os.name");

        //如果是Windows系统
        if (os.toLowerCase().startsWith(BizConstant.SYS_TYPE)) {
            log.info("localUrl="+localUrl);
            registry.addResourceHandler("/uploadData/**")
                    // /uploadData/**表示在磁盘uploadData目录下的所有资源会被解析为以下的路径
                    .addResourceLocations(localUrl);
        } else {  //linux 和mac
            log.info("serverUrl="+serverUrl);
            registry.addResourceHandler("/uploadData/**")
                    .addResourceLocations(serverUrl);
        }

    }





}